#!/bin/bash
# repoid
# custom_repo_name
# custom_repo_addr
# start
# end
# user can add custom_repo_name and custom_repo_addr to setup
# local repo, value can be an array
LKP_SRC=/c/lkp-tests
. $LKP_SRC/lib/debug.sh
. $LKP_SRC/lib/upload.sh
. $LKP_SRC/lib/log.sh

: "${DISTRO:=openeuler}"
: "${basearch:=$(arch)}"
. $LKP_SRC/distro/${DISTRO}

#[ -n "${custom_repo_name}" ]	|| die "custom_repo_name is empty"
#[ -n "${custom_repo_addr}" ]	|| die "custom_repo_addr is empty"

repo_names=($custom_repo_name)
get_rpms()
{
	for i in {1..10}
	do
		echo $i
		#repoquery -a -q >/dev/null 2>&1
		yum install -y -q dnf-plugins-core
		[ $? -eq 0 ] && break
		[ $i -eq 10 ] && die "update repo fail."
	done
	
	#rpms=($(repoquery -aq --queryformat '%{name}' | sed -n '1!p'))
	#rpms=($(repoquery -a -q --queryformat '%{name}' | sed -n '2,$p'))
	#repoid=''
	#for i in "${repo_names[@]}"
	#do
	#	echo $i | grep -E "\-src$" && continue
	#	repoid="$repoid --repoid=$i"	
	#done
	
	#rpms=($(repoquery $repoid -a -q --queryformat '%{name}' | sed -n '2,20p'))
	#rpms=($(repoquery $repoid -a -q --queryformat '%{name}'))
	echo ${start}
	echo ${end}
	#rpms=($(repoquery --repofrompath=${repoid},${repoaddr} --repoid=${repoid} -a -q --queryformat '%{name}' | grep -v '\-debuginfo' | grep -v '\-debugsource' | grep -v '.src$' | sed -n "${start},${end}p"))
	rpms=($(repoquery --repoid=${repoid} -a -q --queryformat '%{name}' | grep -v '\-debuginfo' | grep -v '\-debugsource' | grep -v '.src$' | sed -n "${start},${end}p"))


}

test_rpms()
{
	for i in "${rpms[@]}"
	do

		# test install
		rpm -q "$i"
		if [ $? -eq 0 ]; then
			local rpm_full_name=$(repoquery -q --installed --queryformat '%{name}-%{version}.%{arch}' $i 2>/dev/null)
			echo "start test rpm, rpm name: $i, rpm full name: $rpm_full_name"
			local rpm_src_name=$(repoquery -s -q --installed $i)
			echo "rpm src name, rpm name: $i, rpm src name: $rpm_src_name"
			echo "rpm install result, rpm name: $i, state: pass"
			continue
		fi

		local install_result=1
		for j in {1..10} 
		do
			yum install -y "$i" 2>&1
			install_result=$?
			[ $install_result -eq 0 ] && break
		done

		if [ $install_result -eq 0 ]; then
			# query rpm name
			local rpm_full_name=$(repoquery -q --installed --queryformat '%{name}-%{version}.%{arch}' $i 2>/dev/null)
			echo "start test rpm, rpm name: $i, rpm full name: $rpm_full_name"
			local rpm_src_name=$(repoquery -s -q --installed $i)
			echo "rpm src name, rpm name: $i, rpm src name: $rpm_src_name"
			echo "rpm install result, rpm name: $i, state: pass"
		else
			echo "rpm install result, rpm name: $i, state: fail" && continue
		fi

		# test cmd
		local cmd_list=($(rpm -ql $i | grep -E '/usr/s?bin'))
		[ -z "${cmd_list}" ] || {
			for j in "${cmd_list[@]}"
			do
				strings $j | grep -- --help >/dev/null 2>&1
				if [ $? -eq 0 ]; then
					 timeout 5 $j --help >/dev/null 2>&1
					 if [ $? -eq 0 ]; then
						 echo "run cmd help result, rpm name: $i, cmd: $j, state: pass"
					 else
						 echo "run cmd help result, rpm name: $i, cmd: $j, state: fail"
					 fi
				 fi
			done
		}
		# test service
		local service_list=($(rpm -ql $i | grep -E "\.service$" | awk -F "/" '{print $NF}'))
		[ -z "${service_list}" ] || {
			for j in "${service_list[@]}"
			do
				for k in "start" "status" "stop"
				do
					timeout 10 systemctl ${k} ${j} >/dev/null 2>&1
					if [ $? -ne 0 ]; then
						echo "run service action result, rpm name: $i, service: $j, action: $k, state: fail"
					else
						echo "run service action result, rpm name: $i, service: $j, action: $k, state: pass"
					fi
				done
			done
		}
		# remove rpm
		yum remove -y "$i" 2>&1
		if [ $? -eq 0 ]; then
			echo "rpm uninstall result, rpm name: $i, state: pass"
		else
			echo "rpm uninstall result, rpm name: $i, state: fail"
		fi
	done
}

setup_dns()
{
	echo "start setup dns."
        [ -n "$DNS_HOST" ] || return 0

        echo "nameserver ${DNS_HOST}" > /etc/resolv.conf
}

delete_os_lv()
{
	for i in $(lvdisplay| grep "LV Path"|awk -F' ' '{print $3}');do lvremove -f $i;done
}

uninstall_license()
{
	[[ "$os" = "bclinux" ]] && {
		rpm -e bclinux-license-manager --nodeps
	}	
}

install_repo()
{
	cd /etc/yum.repos.d/
	wget https://gitee.com/spring_view/oec-hardware-rpm/raw/master/repo/openEuler-22.03-LTS-x86_64.repo
	mv openEuler-22.03-LTS-x86_64.repo openEuler.repo
}

setup_dns
delete_os_lv

distro_install_depends install-rpm

uninstall_license
get_rpms
test_rpms
